home *** CD-ROM | disk | FTP | other *** search
- /* ----------------------------------------------------------------------------
-
- File : randgen.c
- Author : Erik Brisson
- James Painter
- Last modified : April 20, 1987
- Purpose : routines for giving random numbers.
-
- -------------------------------------------------------------------------- */
- #include <math.h>
-
- #define IRANGE 2147483647.0 /* 2^31-1 (range of random() is 0 - IRANGE) */
- #define NSTATE_BYTES 32
- int state[NSTATE_BYTES]; /* just has to contain at least NSTATE_BYTES bytes */
-
- unsigned seed;
- int n_gauss = 5;
- double norm_factor;
-
- /* ------------------------------------ */
-
- init_rand(user_seed)
- int user_seed;
- {
- seed = (unsigned) user_seed;
- initstate(seed, state, NSTATE_BYTES);
- norm_factor = sqrt(3.0 / (double)n_gauss);
- return;
- }
-
- /* ------------------------------------ */
-
- restart_random()
- {
- setstate(state);
- srandom(seed);
- return;
- }
-
- /* ------------------------------------ */
-
- int random_int(i1, i2)
- int i1, i2;
- {
- double random_unif();
- int i;
-
- i = (int) random_unif(0.0, (double) (i2-i1)+1);
- if (i < 0) i = 0;
- i += i1;
- if (i > i2) i = i2;
- return i;
- }
-
- /* ------------------------------------ */
-
- double random_unif(low,high)
- double low,high;
- {
- long random();
- return (low + (high-low)*((double)random()/IRANGE));
- }
-
- /* ------------------------------------ */
-
- double gauss(mean,sd)
- double mean, sd;
- {
- int i;
- double x, random_unif();
-
- x = 0;
- for (i=0; i<n_gauss ;i++)
- x += random_unif(-1.0,1.0);
- return (mean + sd*norm_factor*x);
- }
-
-
-